package com.sunda.spmsuser.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunda.spmsuser.entity.SpmsPermission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * spms权限表 Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2021-01-25
 */
@Mapper
@Component
public interface SpmsPermissionMapper extends BaseMapper<SpmsPermission> {

    @Select("SELECT DISTINCT p.* FROM SPMS_USER u,\n" +
            "              SPMS_USER_ROLE ur,\n" +
            "              SPMS_ROLE r,\n" +
            "              SPMS_ROLE_PERMISSION rp,\n" +
            "              SPMS_PERMISSION p\n" +
            "WHERE u.WORK_CODE = ur.WORK_CODE AND ur.ROLE_UUID = r.UUID\n" +
            "  AND r.UUID = rp.ROLE_UUID AND rp.PERMISSION_ID = p.PERMISSION_ID AND u.WORK_CODE = #{userId}")
    List<SpmsPermission> getUserPerm(@Param("userId") String userId);

    @Select("SELECT SP.PERMISSION_ID AS \"permissionId\",\n" +
            "       SP.PARENT_ID AS \"parentId\",\n" +
            "       SP.MENU_NAME AS \"menuName\",\n" +
            "       SP.MENU_EN_NAME AS \"menuEnName\",\n" +
            "       SP.URL AS \"url\",\n" +
            "       SP.COMPONENT_URL AS \"componentUrl\",\n" +
            "       SP.COMPONENT_NAME AS \"componentName\",\n" +
            "       SP.REDIRECT AS \"redirect\",\n" +
            "       SP.MENU_TYPE AS \"menuType\",\n" +
            "       SP.PERMS AS \"perms\",\n" +
            "       SP.PERMS_TYPE AS \"permsType\",\n" +
            "       SP.SORT_NO AS \"sortNo\",\n" +
            "       SP.ALWAYS_SHOW AS \"alwaysShow\",\n" +
            "       SP.ICON AS \"icon\",\n" +
            "       SP.IS_ROUTE AS \"isRoute\",\n" +
            "       SP.IS_LEAF AS \"isLeaf\",\n" +
            "       SP.DESCRIPTION AS \"description\",\n" +
            "       SP.CREATE_TIME AS \"createTime\",\n" +
            "       SP.CREATE_USER AS \"createUser\",\n" +
            "       U.LAST_NAME AS \"createUsername\",\n" +
            "       SP.UPDATE_TIME AS \"updateTime\",\n" +
            "       SP.UPDATE_USER AS \"updateUser\",\n" +
            "       U1.LAST_NAME AS \"updateUsername\"\n" +
            "FROM SPMS_PERMISSION SP\n" +
            "    LEFT JOIN SPMS_USER U ON SP.CREATE_USER = U.WORK_CODE\n" +
            "    LEFT JOIN SPMS_USER U1 ON SP.UPDATE_USER = U1.WORK_CODE")
    List<Map<String, Object>> getAllPerm();

    @Select("SELECT srp.UUID AS \"uuid\",\n" +
            "       srp.ROLE_UUID AS \"roleUuid\",\n" +
            "       srp.PERMISSION_ID AS \"permissionId\"\n" +
            "FROM SPMS_ROLE_PERMISSION srp")
    List<Map<String, Object>> getRolePerm();

//    @Select("SELECT DISTINCT\n" +
//            "                p.PERMISSION_ID AS \"permissionId\",\n" +
//            "                p.PARENT_ID AS \"parentId\",\n" +
//            "                p.MENU_NAME AS \"menuName\",\n" +
//            "                p.MENU_EN_NAME AS \"menuEnName\",\n" +
//            "                p.URL AS \"url\",\n" +
//            "                p.COMPONENT_URL AS \"componentUrl\",\n" +
//            "                p.COMPONENT_NAME AS \"componentName\",\n" +
//            "                p.REDIRECT AS \"redirect\",\n" +
//            "                p.MENU_TYPE AS \"menuType\",\n" +
//            "                p.PERMS AS \"perms\",\n" +
//            "                p.PERMS_TYPE AS \"permsType\",\n" +
//            "                p.SORT_NO AS \"sortNo\",\n" +
//            "                p.ALWAYS_SHOW AS \"alwaysShow\",\n" +
//            "                p.ICON AS \"icon\",\n" +
//            "                p.IS_ROUTE AS \"isRoute\",\n" +
//            "                p.IS_LEAF AS \"isLeaf\"\n" +
//            "FROM SPMS_USER u\n" +
//            "    LEFT JOIN SPMS_USER_ROLE ur ON u.WORK_CODE = ur.WORK_CODE\n" +
//            "    LEFT JOIN SPMS_ROLE r ON ur.ROLE_UUID = r.UUID\n" +
//            "    LEFT JOIN SPMS_ROLE_PERMISSION rp ON r.UUID = rp.ROLE_UUID\n" +
//            "    LEFT JOIN SPMS_PERMISSION p ON rp.PERMISSION_ID = p.PERMISSION_ID\n" +
//            "WHERE p.PERMISSION_ID IS NOT NULL AND u.WORK_CODE = #{userId} ORDER BY p.SORT_NO ASC")
//    List<Map<String, Object>> getPermsByUserId(@Param("userId") String userId);

    @Select("SELECT DISTINCT\n" +
            "                rp.PERMISSION_ID AS \"permissionId\",\n" +
            "                r.ROLE_ID AS \"roleId\",\n" +
            "                ur.UUID AS \"userRoleUuid\",\n" +
            "                r.UUID AS \"roleUuid\",\n" +
            "                r.ROLE_CN_NAME AS \"roleCnName\",\n" +
            "                r.ROLE_EN_NAME AS \"roleEnName\"\n" +
            "FROM SPMS_USER u\n" +
            "    LEFT JOIN SPMS_USER_ROLE ur ON u.WORK_CODE = ur.WORK_CODE\n" +
            "    LEFT JOIN SPMS_ROLE r ON ur.ROLE_UUID = r.UUID\n" +
            "    LEFT JOIN SPMS_ROLE_PERMISSION rp ON r.UUID = rp.ROLE_UUID\n" +
            "WHERE rp.PERMISSION_ID IS NOT NULL AND u.WORK_CODE = #{userId} ORDER BY r.ROLE_ID ASC")
    List<Map<String, Object>> getPermsByUserId(@Param("userId") String userId);
}
